﻿/*****************************************************
 * 本类库的核心系 JNTemplate
 * (C) JNTemplate 2011
 * 作者：翅膀的初衷 QQ:4585839
 * 网址：http://www.JiNianNet.com
 *****************************************************/
using System;
using System.Collections.Generic;

namespace JinianNet.JNTemplate.Process.Element
{
    public class FunctaionTag : SimpleTag
    {

        public FunctaionTag(string text)
            : base(TokenKind.Function, text)
        {

        }
        //private string _key;
        //public string Key
        //{
        //    get { return this._key; }
        //    set { this._key = value; }
        //}
        private string _parameter;
        public string Parameter
        {
            get { return this._parameter; }
            set { this._parameter = value; }
        }
        private string _method;
        public string Method
        {
            get { return this._method; }
            set { this._method = value; }
        }



        public override object Parser(VariableScope varScope)
        {

            try
            {
                Parsing word = new Parsing();
                List<string> args = new List<string>();

                if (!string.IsNullOrEmpty(_parameter))
                {
                    List<object> array = new TemplateParser().Split(this._parameter, true, new char[] { '+', ',' });
                    if (array.Count > 0)
                    {
                        args.Add(string.Empty);
                        for (int i = 0; i < array.Count; i++)
                        {
                            if (array[i] is SimpleTag)
                            {
                                array[i] = (((Token)array[i]).Parser(varScope));
                            }
                            if (array[i].ToString() == ",")
                            {
                                args.Add(string.Empty);
                            }
                            else if (array[i].ToString() == "+")
                            {

                            }
                            else
                            {
                                args[args.Count - 1] = string.Concat(args[args.Count - 1], array[i].ToString());
                            }
                        }
                    }
                }


                FuncHandler func;
                if (VariableScope.HandlerCollection.TryGetValue(this._method, out func))
                {
                    if (func != null)
                    {
                        return func.Invoke(args.ToArray());
                    }
                }

                object value = varScope[this._method];
                if (value != null)
                {
                    if (value is FuncHandler)
                    {
                        return (value as FuncHandler).Invoke(args.ToArray());
                    }
                }

                foreach (object cs in VariableScope.ClassCollection)
                {
                    System.Reflection.MethodInfo method = varScope.EvalMethod(cs.GetType(), this._method, null);
                    if (method != null)
                    {
                        return method.Invoke(cs, args.ToArray());
                    }
                }

            }
            catch (Exception e)
            {
                return e.Message;
            }
            return "未定义的方法";
        }
    }
}
